{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python Exception Handling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unsupported operand type(s) for +: 'int' and 'str'\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    ##code block where exception can occur\n",
    "    a=1\n",
    "    b=\"s\"\n",
    "    c=a+b\n",
    "except NameError as ex1:\n",
    "    print(\"The user have not defined the variable\")\n",
    "except Exception as ex:\n",
    "    print(ex)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'b' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-488068fe1e94>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'b' is not defined"
     ]
    }
   ],
   "source": [
    " a=b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-9b96ead189f1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"s\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mc\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "a=1\n",
    "b=\"s\"\n",
    "c=a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the number 1 12\n",
      "Enter the number 2 0\n",
      "Please provide number greater than 0\n"
     ]
    }
   ],
   "source": [
    "### try else\n",
    "try:\n",
    "    ##code block where exception can occur\n",
    "    a=int(input(\"Enter the number 1 \"))\n",
    "    b=int(input(\"Enter the number 2 \"))\n",
    "    c=a/b \n",
    "    d=a*b\n",
    "    e=a+b\n",
    "    \n",
    "except NameError:\n",
    "    print(\"The user have not defined the variable\")\n",
    "except ZeroDivisionError:\n",
    "    print(\"Please provide number greater than 0\")\n",
    "except TypeError:\n",
    "    print(\"Try to make the datatype similar\")\n",
    "except Exception as ex:\n",
    "    print(ex)\n",
    "else:\n",
    "    print(c)\n",
    "    print(d)\n",
    "    print(e)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-898e9759c56e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m12\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "##try else finally\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the number 1 12\n",
      "Enter the number 2 0\n",
      "Please provide number greater than 0\n",
      "The execution is done\n"
     ]
    }
   ],
   "source": [
    "### try else\n",
    "try:\n",
    "    ##code block where exception can occur\n",
    "    a=int(input(\"Enter the number 1 \"))\n",
    "    b=int(input(\"Enter the number 2 \"))\n",
    "    c=a/b\n",
    "    \n",
    "except NameError:\n",
    "    print(\"The user have not defined the variable\")\n",
    "except ZeroDivisionError:\n",
    "    print(\"Please provide number greater than 0\")\n",
    "except TypeError:\n",
    "    print(\"Try to make the datatype similar\")\n",
    "except Exception as ex:\n",
    "    print(ex)\n",
    "else:\n",
    "    print(c)\n",
    "finally:\n",
    "    print(\"The execution is done\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Custom Exception"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Error(Exception):\n",
    "    pass\n",
    "\n",
    "class dobException(Error):\n",
    "    pass\n",
    "\n",
    "class customgeneric(Error):\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter the year of Birth 1986\n",
      "The year age is not within the range. You cannot apply for the exams\n"
     ]
    }
   ],
   "source": [
    "year=int(input(\"Enter the year of Birth \"))\n",
    "age=2021-year\n",
    "try:\n",
    "    if age<=30 & age>20:\n",
    "        print(\"The age is valid. You can apply for the exams\")\n",
    "    else:\n",
    "        raise dobException\n",
    "except dobException:\n",
    "    print(\"The age is not within the range. You cannot apply for the exams\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "    \n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
